package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Bill;
import cn.zucc.edu.handyzucc.entity.BillExtend;
import cn.zucc.edu.handyzucc.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;

    @RequestMapping(value = "getBillsByDorm", method = RequestMethod.POST)
    private Map<String, Object> getBillsByDorm(Integer bill_dorm) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Bill> bills = billService.getBillsByDorm(bill_dorm);
//        System.out.println(bills.get(0).getBill_deadline());
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", bills);
        return modelMap;
    }

    @RequestMapping(value = "getAllBill", method = RequestMethod.GET)
    private Map<String, Object> getAllBill() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Bill> bills = billService.getAllBill();
        modelMap.put("code", "0");
        modelMap.put("data", bills);
        return modelMap;
    }

    @RequestMapping(value = "deleteBill", method = RequestMethod.POST)
    private Map<String, Object> deleteBill(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (billService.deleteBill(Integer.parseInt(map.get("bill_id").toString())))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "updateBill", method = RequestMethod.POST)
    private Map<String, Object> updateBill(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Bill bill = new Bill(Integer.parseInt(map.get("bill_id").toString()), Integer.parseInt(map.get("bill_dorm").toString()), Float.parseFloat(map.get("bill_amount").toString()), map.get("bill_period").toString());
        if (billService.updateBill(bill))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "batchAddBill", method = RequestMethod.POST)
    private Map<String, Object> batchAddBill(@RequestParam("file") MultipartFile multipartFile) {
        Map<String, Object> modelMap = new HashMap<>();
        List<BillExtend> bills = billService.batchAddBill(multipartFile);
        modelMap.put("code", "0");
        modelMap.put("data", bills);
        return modelMap;
    }
}
